$LOAD_PATH << "."
$LOAD_PATH << "./scripts"
$LOAD_PATH << "./tool"

# Star Ruby
require 'starruby'
require 'resource_manager'
require "extension"
$res = ResourceManager.new(".", "/images/", "/sounds/se/", "/sounds/bgm/", "/data/", "/tool/")
$buffer = {}

# Gnome2
require 'gtk2'
Gtk::RC.parse($res.tool_path + "theme/gtkrc")

require 'editor_config'
$config = EditorConfig.new(16, 16, 11)

require 'main_container'
require 'map_editor/io'
require 'event_editor/io'


def add_tool_toggle_button(tool_buttons, toolbar, icon_path, name, group, &block)
  tool_buttons[group] = [] unless tool_buttons[group]
  
  b = Gtk::ToggleButton.new
  tool_buttons[group] << b
  b.add(Gtk::Image.new($res.tool_path + icon_path))

  b.signal_connect("pressed") do
    if tool_buttons[group].length > 1
      (tool_buttons[group]).each{|t| t.active = false }
    end
    block.call
  end

  toolbar.append(b, name, name)
  return b
end

def add_tool_button(toolbar, name, icon_path, &block)
  b = Gtk::Button.new
  b.add(Gtk::Image.new($res.tool_path + icon_path))

  b.signal_connect("clicked") do
    block.call
  end

  toolbar.append(b, name, name)
  return b
end

def getPixmap(path)
  tex = StarRuby::Texture.load($res.tool_path + path)
  pixbuf = Gdk::Pixbuf.new(tex.dump('rgb'), Gdk::Pixbuf::ColorSpace.new(Gdk::Pixbuf::ColorSpace::RGB), false, 8, tex.width, tex.height, tex.width * 3)
  pixmap, mask = pixbuf.render_pixmap_and_mask(0)
  return pixmap
end

def set_background_image(path, target)
  pixmap = getPixmap(path)

  style = target.style
  style.set_bg_pixmap(Gtk::STATE_NORMAL, pixmap)
  style.set_bg_pixmap(Gtk::STATE_ACTIVE, pixmap)
  style.set_bg_pixmap(Gtk::STATE_PRELIGHT, pixmap)
  style.set_bg_pixmap(Gtk::STATE_INSENSITIVE, pixmap)
  style.set_bg_pixmap(Gtk::STATE_SELECTED, pixmap)
  
  target.style = style
end

class Main
  def initialize
    @tool_buttons = {}
    window = Gtk::Window.new
    $window = window

    window.title = "Map Editor"
    window.signal_connect("delete_event") do
    	Gtk::main_quit
    	false
    end

    vbox = Gtk::VBox.new
    table = Gtk::Table.new(1, 4, false)  
    
    menubar = create_menubar
    table.attach(menubar, 0, 1, 0, 1, Gtk::EXPAND | Gtk::FILL, 0)
    
    toolbar = create_toolbar
    toolbar.set_height_request(32)
    table.attach(toolbar, 0, 1, 1, 2, Gtk::EXPAND | Gtk::FILL, 0)
    
    @main_container = MainContainer.new
    
    
    table.attach(@main_container, 0, 1, 2, 3, Gtk::EXPAND | Gtk::FILL, Gtk::EXPAND | Gtk::FILL)

    statusbar = Gtk::Statusbar.new
    table.attach(statusbar, 0, 1, 3, 4, Gtk::EXPAND | Gtk::FILL, 0)

    window.add(table)
    
    window.set_width_request(792)
    window.set_height_request(600)
    
    window.show_all

    window.signal_connect("configure_event") do |item, event|
    	#@main_container.on_resize(event.width, event.height - 86)
    end
    Gtk.main
  end

  def create_menubar
    menubar = Gtk::MenuBar.new

    # menuitem = Gtk::MenuItem.new("File")
    # menubar.append(menuitem)
    # menuitem.show

    # menuitem = Gtk::MenuItem.new('Config')
    # menubar.append(menuitem)
    # menuitem.show
    
    return menubar
  end

  def create_toolbar
    toolbar = Gtk::Toolbar.new

    
    return toolbar
  end
end

Main.new